package com.example.module.custom.utils;

import android.content.Context;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

import com.example.module.custom.CustomNewApp;

import static com.lzy.okgo.utils.HttpUtils.runOnUiThread;

/**
 * @name BlockDemo
 * @class name：com.example.module.custom.utils
 * @classdescribe
 * @anthor ljp
 * @time 2020/1/8 17:35
 */
public class ToastUtil {

    private static Toast toast;//实现不管我们触发多少次Toast调用，都只会持续一次Toast显示的时长

    /**
     * 短时间显示Toast【居下】
     *
     * @param msg 显示的内容-字符串
     */
    public static void showShortToast(String msg, Context context) {
        try {
            if (null == toast) {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    toast.show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
    }

   /* *//**
     * 短时间显示Toast【居中】
     *
     * @param msg 显示的内容-字符串
     *//*
    public static void showShortToastCenter(String msg,Context context) {
        try {
            if (null == toast) {
                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    toast.show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            //解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
    }

    *//**
     * 短时间显示Toast【居上】
     *
     * @param msg 显示的内容-字符串
     *//*
    public static void showShortToastTop(String msg) {
        if (CustomNewApp.getInstance() != null) {
            if (toast == null) {
                toast = Toast.makeText(CustomNewApp.getInstance(), msg, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP, 0, 0);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }

    *//**
     * 长时间显示Toast【居下】
     *
     * @param msg 显示的内容-字符串
     *//*
    public static void showLongToast(String msg) {
        if (CustomNewApp.getInstance() != null) {
            if (toast == null) {
                toast = Toast.makeText(CustomNewApp.getInstance(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }

    *//**
     * 长时间显示Toast【居中】
     *
     * @param msg 显示的内容-字符串
     *//*
    public static void showLongToastCenter(String msg) {
        if (CustomNewApp.getInstance() != null) {
            if (toast == null) {
                toast = Toast.makeText(CustomNewApp.getInstance(), msg, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }

    *//**
     * 长时间显示Toast【居上】
     *
     * @param msg 显示的内容-字符串
     *//*
    public static void showLongToastTop(String msg) {
        if (CustomNewApp.getInstance() != null) {
            if (toast == null) {
                toast = Toast.makeText(CustomNewApp.getInstance(), msg, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 0, 0);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }*/
}
